package cn.web.controller;

import cn.constant.ApiConstant;
import cn.constant.Messages;
import cn.entity.RealName;
import cn.entity.User;
import cn.service.RealNameService;
import cn.service.UserService;
import cn.util.AuthUtils;
import cn.util.StringUtils;
import cn.web.model.ApiResult;
import cn.web.model.VmUser;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author m on 2023/2/1
 * Description:
 */
@Api(tags = "个人用户业务")
@RestController
@RequestMapping(ApiConstant.ROUTE_USER_ROOT)
public class UserApiController {
    private final UserService userService;
    private final RealNameService realNameService;

    @Autowired
    public UserApiController(UserService userService, RealNameService realNameService) {
        this.userService = userService;
        this.realNameService = realNameService;
    }

    @ApiOperation("更新个人信息(除头像)")
    @PutMapping(value = "/update",produces = ApiConstant.API_PRODUCES)
    public ApiResult updateUser(@RequestBody JSONObject json){
        User user = userService.getById(AuthUtils.getUser(json.getString(ApiConstant.KEY_TOKEN)).getId());
        if (user == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(),null);
        }

        String nick = json.getString(ApiConstant.KEY_REGISTER_NICK_NAME);
        String name = json.getString(ApiConstant.KEY_REGISTER_USER_NAME);
        String phone = json.getString(ApiConstant.KEY_REGISTER_PHONE);
        String email = json.getString(ApiConstant.KEY_REGISTER_EMAIL);
        boolean apply = json.getBoolean(ApiConstant.KEY_REGISTER_APPLY);
        //不为空则更新
        if(!StringUtils.isEmpty(nick)){
            user.setNickName(nick);
        }
        if(!StringUtils.isEmpty(phone)){
            user.setPhone(phone);
        }
        if(!StringUtils.isEmpty(email)){
            user.setEmail(email);
        }
        System.out.println("222");
        if(apply!=user.isApply()){
            user.setApply(apply);
        }
        System.out.println("333");
        if(!StringUtils.isEmpty(name)){
            user.setUserName(name);
        }
        user.setUpdateTime(new Date());
        userService.register(user);
        return new ApiResult(true, Messages.SUCCESS.toString(), VmUser.create(user,false));
    }

    @ApiOperation("上传头像")
    @PostMapping(value = "/upload")
    public ApiResult uploadAvatar(@RequestParam("file")MultipartFile file, String token){
        Messages msg;
        try{
            msg = userService.uploadAvatar(file,token);
            if (Messages.SUCCESS.equals(msg)) {
                return new ApiResult(true,msg.toString(),file.getName());
            }else{
                return new ApiResult(false,msg.toString(),null);
            }
        }catch (Exception e){
            return new ApiResult(false,"上传失败",e.getMessage());
        }
    }

    //实名
    @ApiOperation("实名")
    @PostMapping(value = "/realName",produces = ApiConstant.API_PRODUCES)
    public ApiResult realName(@RequestBody JSONObject json){
        User user = userService.getById(AuthUtils.getUser(json.getString(ApiConstant.KEY_TOKEN)).getId());
        RealName realName = new RealName();
        realName.setName(json.getString(ApiConstant.KEY_REAL_NAME));
        realName.setNumber(json.getString(ApiConstant.KEY_REAL_NUMBER));
        realName.setUser_id(user.getId());
        user.setRealname(realName);
        return new ApiResult(true, Messages.SUCCESS.toString(),VmUser.create(user,true));
    }
}
